import 'package:flutter/material.dart';
import 'package:flutter_base/utils/navigator_utils.dart';
import 'package:flutter_base/viewmodel/hot/search_vm.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:provider/provider.dart';

import '../../common/common_style.dart';
import '../../common/webview_page.dart';
import '../../common/webview_widget.dart';

///
/// @DIR_PATH：lib/bean/hot/search
/// @TIME：2024/6/13 16:19
/// @AUTHOR：starr
/// 搜索页面
///
class SearchPage extends StatefulWidget{
  const SearchPage({super.key,required this.keyword});
  final String keyword;

  @override
  State<StatefulWidget> createState() => _SearchPageState();

}

class _SearchPageState extends State<SearchPage>{

  late TextEditingController controller;

  SearchViewModel viewModel = SearchViewModel();

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
        create: (context) {
          return viewModel;
        },
        child: Scaffold(
          body: SafeArea(
              child: Column(
            children: [
              _searchWidget(),
              _listView(),
            ],
          )),
        ));
  }

  @override
  void initState() {
    super.initState();
    controller = TextEditingController(text: widget.keyword);
    viewModel.search(widget.keyword);
  }
  Widget _searchWidget(){
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 10),
      color: Colors.teal,
      height: 50,
      width: double.infinity,
      child: Row(
        children: [
          const SizedBox(width: 5,),
          GestureDetector(onTap: () {
            NavigatorUtils.pop(context);
          },child:const Icon(Icons.arrow_back_ios_new,color: Colors.white,),),
          Expanded(child: Container(
            padding: const EdgeInsets.all(6),
            child: commonInput(
              controller:controller,
              icon: const Icon(Icons.search),
              onSubmitted: (value){
                viewModel.search(value);
              },
            ),
          )),
          GestureDetector(
            onTap: (){
              controller.text ="";
              viewModel.clear();
            },
            child: const Text("取消",style: TextStyle(fontSize: 13,color: Colors.white),),
          )
        ],
      ),
    );
  }

  Widget _listView() {
    return Consumer<SearchViewModel>(builder: (context, vm, child) {
      return Expanded(
          child: ListView.builder(
              itemCount: vm.searchList?.length ?? 0,
              itemBuilder: (context, index) {
                return GestureDetector(
                  onTap: () {
                    NavigatorUtils.openPageByFade(
                        context,
                        WebViewPage(
                          title: vm.searchList?[index].title ?? "",
                          showTitle: true,
                          loadResource: vm.searchList?[index].link ?? "",
                          webViewType: WebViewType.URL,
                        ));
                  },
                  child: Container(
                    padding:
                        const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
                    decoration: const BoxDecoration(
                        border: Border(
                            bottom: BorderSide(width: 1, color: Colors.grey))),
                    child: Html(
                      data: vm.searchList?[index].title ?? "",
                      style: {"html": Style(fontSize: FontSize(18))},
                    ),
                  ),
                );
              }));
    });
  }
}